package com.qf.lanzhou.blog.api.dao.Impl;

import com.qf.lanzhou.blog.api.dao.FeedbackDao;
import com.qf.lanzhou.blog.api.pojo.entity.FeedbackEntity;
import com.qf.lanzhou.blog.api.util.BeanProcessor;
import com.qf.lanzhou.blog.api.util.DruidUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author jack
 */
public class FeedbackDaolmpl implements FeedbackDao {
    private QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());

    @Override
    public List selectAll() {
        String sql = "SELECT * FROM t_feedback";
        BeanListHandler<Object> beanListHandler = BeanProcessor.getBeanListHandler(FeedbackEntity.class);
        try {
            List list = queryRunner.query(sql, beanListHandler);
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public int insert(FeedbackEntity feedback) {
        String sql = "INSERT INTO `blog`.`t_feedback`( `user_name`, `feedback`, " +
                "`fb_satisfaction`, `fb_phone_number`, `fb_favorite_blogger` )" +
                " VALUES ( ?,?,?,?,? );";
        Object[] parm = {feedback.getUserName(),feedback.getFeedback(),feedback.getFbSatisfaction(),
        feedback.getFbPhoneNumber(),feedback.getFbFavoriteBlogger()};
        try {
            int i = queryRunner.update(sql, parm);
            return i;
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }
}
